// pages/MyReport/Myreport.js
const api = require("../../config/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    reportList: [],
    psCustomerCode: '',
    typeOptions: ['类型', '待处理', '驳回', '留观', '已处置'], // 下拉选项
    selectedType: '类型',
    feedbackState: -1,
  },

  onMyEstimation: function (e) {
    const reportIndex = e.currentTarget.dataset.index; // 获取当前点击的报告的索引
    const selectedReport = this.data.reportList[reportIndex];
    console.log('selectedReport')
    console.log(selectedReport)
    //页面跳转传参
    wx.navigateTo({
      url: `/pages/MyPage/MyEstimation/MyEstimation?feedbackCode=${selectedReport.feedbackCode}`
    });
  },
  onValue: function (e) {
    wx.navigateTo({
      url: '/pages/MyReport/value/value',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  onShow() {
    this.setData({
      pageNum: 1,
      reportList: [],
      feedbackState: '',
    })
    //查询
    this.fetchReportData();
  },

  fetchReportData: function (params = {}) {
    wx.getStorage({
      key: 'psCustomerCode',
      success: (res) => {
        this.setData({
          psCustomerCode: res.data
        });
        wx.request({
          url: api.feedbackAll,
          method: 'POST',
          data: {
            psCustomerCode: this.data.psCustomerCode,
            pageNum: this.data.pageNum,
            pageSize: "5",
            feedbackState: this.data.feedbackState,
            feedbackType: 3,
            feedbackClassification:2,
            feedbackState:-1,
          },
          success: (res) => {
            wx.hideLoading()
            if (res.data.code == 200) {
              console.log("我的上报数据：")
              console.log(res.data.data)
              const reportData = res.data.data.rows;
              console.log('length:',reportData.length)
              if (reportData.length!=0) {
                this.setData({
                  pageNum: this.data.pageNum + 1,
                });
              }
              this.setData({
                reportList: [...this.data.reportList, ...reportData] // 将 reportData 追加到 reportList 的末尾
              });
            } else {
              wx.showToast({
                title: res.data.mesage,
                icon: "none"
              })
            }
          }
        })
      },
    });
  },

  onReportDetail: function (e) {
    const reportIndex = e.currentTarget.dataset.index; // 获取当前点击的报告的索引
    const selectedReport = this.data.reportList[reportIndex];
    console.log('reportIndex')
    console.log(reportIndex)
    //页面跳转传参
    wx.navigateTo({
      url: `/pages/MyReport/ReportDetail/ReportDetail?feedbackCode=${selectedReport.feedbackCode}`
    });
  },
  // 处理类型选择事件
  onTypeChange: function (e) {
    const index = e.detail.value; // 获取选中的下标
    console.log('index:', index)
    this.setData({
      pageNum: 1,
      reportList: [],
    })
    this.setData({
      selectedType: this.data.typeOptions[index], // 更新选中类型
      feedbackState: index,
    });
    this.fetchReportData();
  },

  filterReports: function (selectedType) {
    // 根据 `selectedType` 执行筛选逻辑，比如请求新的数据或过滤现有数据
    console.log('根据类型筛选:', selectedType);
  },
  // 滑到底部
  onScrollToLower: function () {
    console.log('滑到末尾，刷新')
    this.fetchReportData()
  },
  // 重新获取数据
  onScrollToUpper: function () {
    console.log('刷新')
    this.resetData()
  },
  resetData: function () {
    this.setData({
      pageNum: 1,
      reportList: [],
      feedbackState: '',
    })
    //查询
    this.fetchReportData();
  }
})